#!/usr/bin/env bash

function docs {
    pushd docs && \
        rm -rf public && \
        hexo generate && \
        surge public lang-study.surge.sh && \
        popd
}

function t {
    rm -f target/universal/stage/bin/*

    sbt stage && \
        test_general && \
        test_nbe && \
        test_barendregt && \
        test_eopl && \
        test_adventure
}

function test_general {
    for file in $(find . | grep "^\./target/universal/stage/bin/" | grep "test$")
    do
        echo ""
        echo "[test] $file"
        if ! time $file
        then
            exit 1
        fi
    done
}

function test {
    name=$1

    prog=./target/universal/stage/bin/${name}

    for file in $(find . | grep "^\./example/${name}/" | grep "\.${name}$")
    do
        echo ""
        echo "[${name}] $file"
        if ! time $prog --eval $file
        then
            exit 1
        fi
    done
}

function test_nbe {
    test_nbe_lang lambda &&\
        test_nbe_lang syst &&\
        test_nbe_lang tartlet &&\
        test_nbe_lang minitt
}

function test_nbe_lang {
    name=$1

    prog=./target/universal/stage/bin/${name}

    for file in $(find . | grep "^\./example/nbe/${name}/")
    do
        echo ""
        echo "[${name}] $file"
        if ! time $prog --eval $file
        then
            exit 1
        fi
    done
}

function test_eopl {
    test_eopl_lang lang_let &&\
        test_eopl_lang lang_proc &&\
        test_eopl_lang lang_nameless &&\
        test_eopl_lang lang_letrec &&\
        test_eopl_lang lang_explicit_refs &&\
        test_eopl_lang lang_implicit_refs &&\
        test_eopl_lang lang_mutable_pairs &&\
        test_eopl_lang lang_call_by_reference &&\
        test_eopl_lang lang_call_by_need &&\
        test_eopl_lang lang_checked &&\
        test_eopl_lang lang_infered
    # test_eopl_lang lang_module
    # test_eopl_lang lang_class
}

function test_eopl_lang {
    name=$1

    prog=./target/universal/stage/bin/${name}

    for file in $(find . | grep "^\./example/eopl/${name}/" | grep "\.eopl$")
    do
        echo ""
        echo "[${name}] $file"
        if ! time $prog --eval $file
        then
            exit 1
        fi
    done
}

function test_barendregt {
    test_barendregt_lang de_bruijn
}

function test_barendregt_lang {
    name=$1

    prog=./target/universal/stage/bin/${name}

    for file in $(find . | grep "^\./example/barendregt/${name}/")
    do
        echo ""
        echo "[${name}] $file"
        if ! time $prog --eval $file
        then
            exit 1
        fi
    done
}

function test_adventure {
    test_adventure_jojo jojo_untyped &&\
        test_adventure_jojo jojo_simple
    # test_adventure_jojo jojo_dependent
}

function test_adventure_jojo {
    name=$1

    prog=./target/universal/stage/bin/${name}

    for file in $(find . | grep "^\./example/adventure/${name}/" | grep "\.jo$")
    do
        echo ""
        echo "[${name}] $file"
        if ! time $prog --eval $file
        then
            exit 1
        fi
    done
}

function main {
    for TASK in $@
    do
        $TASK
    done
}

main $@
